import { Ref, computed } from "vue";

import { getListApi } from "@/api/financialManagement/payableManagement/payableDetails";
export default function OperationHook(state: {
  searchForm: PayableDetails.SearchForm;
  tableData: Ref<PayableDetails.ListRows[]>;
  tableRef: Ref<any>;
  loading: Ref<boolean>;
  pageStat: Ref<Base.GeneraDataType>;
  drawerOpts: Base.DrawerOpts;
}) {
  const { searchForm, tableData, loading, tableRef, drawerOpts } = state;
  const selectRows = computed<Array<StatementPayable.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });

  const getData = async () => {
    const params: Parameters<typeof getListApi>[0] = {
      ...getParams()
    };
    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const getParams = () => {
    const { outWarehouseBrokerIdList, checkingStatusList, companyIdList, waybillNumList, beforePageNum, pageNum, pageSize } = searchForm;
    const params: any = {
      outWarehouseBrokerIdList,
      checkingStatusList,
      companyIdList,
      beforePageNum,
      pageNum,
      pageSize
    };
    if (waybillNumList)
      params.waybillNumList = waybillNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");

    return params;
  };

  const searchHandle = async () => {
    searchForm.pageNum = 1;
    getData();
  };
  // 详情 Details
  const getInfoHandle = (row: AgencyBalance.ListRows) => {
    drawerOpts.title = "详情";
    drawerOpts.width = "1200px";
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  return {
    searchHandle,
    getData,
    selectRows,
    getInfoHandle
  };
}
